﻿namespace S7.Net
{
    using S7.Net.Types;
    using System;
    using System.Globalization;
    using System.Runtime.CompilerServices;

    public static class Conversion
    {
        public static string asc_to_hex(string asc)
        {
            string str2 = "";
            for (int i = 0; i < asc.Length; i++)
            {
                string str = ((short) char.Parse(asc.Substring(i, 1))).ToString("X2");
                str2 = str2 + str;
            }
            return str2;
        }

        public static byte? BinStringToByte(this string txt)
        {
            int startIndex = 0;
            int num2 = 0;
            if (txt.Length == 8)
            {
                for (startIndex = 7; startIndex >= 0; startIndex += -1)
                {
                    if (int.Parse(txt.Substring(startIndex, 1)) == 1)
                    {
                        num2 += (int) Math.Pow(2.0, (double) ((txt.Length - 1) - startIndex));
                    }
                }
                return new byte?((byte) num2);
            }
            return null;
        }

        public static int BinStringToInt32(this string txt)
        {
            int startIndex = 0;
            int num2 = 0;
            for (startIndex = txt.Length - 1; startIndex >= 0; startIndex += -1)
            {
                if (int.Parse(txt.Substring(startIndex, 1)) == 1)
                {
                    num2 += (int) Math.Pow(2.0, (double) ((txt.Length - 1) - startIndex));
                }
            }
            return num2;
        }

        public static string byteToHexStr(byte[] bytes, int len)
        {
            string str = "";
            if (bytes != null)
            {
                for (int i = 0; i < len; i++)
                {
                    str = str + bytes[i].ToString("X2");
                }
            }
            return str;
        }

        public static double ConvertToDouble(this uint input)
        {
            return S7.Net.Types.Double.FromByteArray(DWord.ToByteArray(input));
        }

        public static int ConvertToInt(this uint input)
        {
            return int.Parse(input.ToString("X"), NumberStyles.HexNumber);
        }

        public static short ConvertToShort(this ushort input)
        {
            return short.Parse(input.ToString("X"), NumberStyles.HexNumber);
        }

        public static uint ConvertToUInt(this double input)
        {
            return DWord.FromByteArray(S7.Net.Types.Double.ToByteArray(input));
        }

        public static uint ConvertToUInt(this int input)
        {
            return uint.Parse(input.ToString("X"), NumberStyles.HexNumber);
        }

        public static ushort ConvertToUshort(this short input)
        {
            return ushort.Parse(input.ToString("X"), NumberStyles.HexNumber);
        }

        public static string hex_to_asc(string hex)
        {
            string str3 = "";
            hex = hex.Replace(" ", "");
            for (int i = 0; i < (hex.Length / 2); i++)
            {
                string str2 = ((char) Convert.ToInt32("0x" + hex.Substring(i * 2, 2), 0x10)).ToString();
                str3 = str3 + str2;
            }
            return str3;
        }

        public static bool SelectBit(this byte data, int bitPosition)
        {
            int num = ((int) 1) << bitPosition;
            int num2 = data & num;
            return (num2 != 0);
        }

        public static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
            {
                hexString = hexString + " ";
            }
            byte[] buffer = new byte[hexString.Length / 2];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 0x10);
            }
            return buffer;
        }

        public static string ValToBinString(this object value)
        {
            string str2;
            int num = 0;
            int index = 0;
            int num3 = 0;
            string str = "";
            long num4 = 0L;
            try
            {
                if (value.GetType().Name.IndexOf("[]") >= 0)
                {
                    goto Label_0116;
                }
                string name = value.GetType().Name;
                if (name == null)
                {
                    goto Label_00B8;
                }
                if (!(name == "Byte"))
                {
                    if (name == "Int16")
                    {
                        goto Label_008F;
                    }
                    if (name == "Int32")
                    {
                        goto Label_009D;
                    }
                    if (name == "Int64")
                    {
                        goto Label_00AB;
                    }
                    goto Label_00B8;
                }
                num3 = 7;
                num4 = (byte) value;
                goto Label_00BE;
            Label_008F:
                num3 = 15;
                num4 = (short) value;
                goto Label_00BE;
            Label_009D:
                num3 = 0x1f;
                num4 = (int) value;
                goto Label_00BE;
            Label_00AB:
                num3 = 0x3f;
                num4 = (long) value;
                goto Label_00BE;
            Label_00B8:
                throw new Exception();
            Label_00BE:
                num = num3;
                while (num >= 0)
                {
                    if ((num4 & ((long) Math.Pow(2.0, (double) num))) > 0L)
                    {
                        str = str + "1";
                    }
                    else
                    {
                        str = str + "0";
                    }
                    num += -1;
                }
                goto Label_0368;
            Label_0116:
                name = value.GetType().Name;
                if (name == null)
                {
                    goto Label_0361;
                }
                if (!(name == "Byte[]"))
                {
                    if (name == "Int16[]")
                    {
                        goto Label_01ED;
                    }
                    if (name == "Int32[]")
                    {
                        goto Label_026A;
                    }
                    if (name == "Int64[]")
                    {
                        goto Label_02E7;
                    }
                    goto Label_0361;
                }
                num3 = 7;
                byte[] buffer = (byte[]) value;
                for (index = 0; index <= (buffer.Length - 1); index++)
                {
                    num = num3;
                    while (num >= 0)
                    {
                        if ((buffer[index] & ((byte) Math.Pow(2.0, (double) num))) > 0)
                        {
                            str = str + "1";
                        }
                        else
                        {
                            str = str + "0";
                        }
                        num += -1;
                    }
                }
                goto Label_0368;
            Label_01ED:
                num3 = 15;
                short[] numArray = (short[]) value;
                for (index = 0; index <= (numArray.Length - 1); index++)
                {
                    num = num3;
                    while (num >= 0)
                    {
                        if ((numArray[index] & ((byte) Math.Pow(2.0, (double) num))) > 0)
                        {
                            str = str + "1";
                        }
                        else
                        {
                            str = str + "0";
                        }
                        num += -1;
                    }
                }
                goto Label_0368;
            Label_026A:
                num3 = 0x1f;
                int[] numArray2 = (int[]) value;
                for (index = 0; index <= (numArray2.Length - 1); index++)
                {
                    num = num3;
                    while (num >= 0)
                    {
                        if ((numArray2[index] & ((byte) Math.Pow(2.0, (double) num))) > 0)
                        {
                            str = str + "1";
                        }
                        else
                        {
                            str = str + "0";
                        }
                        num += -1;
                    }
                }
                goto Label_0368;
            Label_02E7:
                num3 = 0x3f;
                byte[] buffer2 = (byte[]) value;
                for (index = 0; index <= (buffer2.Length - 1); index++)
                {
                    for (num = num3; num >= 0; num += -1)
                    {
                        if ((buffer2[index] & ((byte) Math.Pow(2.0, (double) num))) > 0)
                        {
                            str = str + "1";
                        }
                        else
                        {
                            str = str + "0";
                        }
                    }
                }
                goto Label_0368;
            Label_0361:
                throw new Exception();
            Label_0368:
                str2 = str;
            }
            catch
            {
                str2 = "";
            }
            return str2;
        }
    }
}

